package com.jz.community.moduleshoppingguide.home.model;

import android.content.Context;

import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.JsonRequestBodyConverter;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.moduleshoppingguide.home.bean.CategoriesGoodsBean;
import com.jz.community.moduleshoppingguide.home.bean.ChunnelCategoriesBean;
import com.jz.community.moduleshoppingguide.home.bean.ChunnelNewbornBean;
import com.jz.community.moduleshoppingguide.home.bean.ChunnelTypeBean;
import com.jz.community.moduleshoppingguide.home.bean.HomeFindBean;
import com.jz.community.moduleshoppingguide.home.bean.HomeRecommendBean;
import com.jz.community.moduleshoppingguide.home.bean.HotSaleAllBean;
import com.jz.community.moduleshoppingguide.home.bean.HotSaleShowBean;
import com.jz.community.moduleshoppingguide.home.bean.LimitGoodsBean;
import com.jz.community.moduleshoppingguide.home.bean.PreferentialBean;
import com.jz.community.moduleshoppingguide.home.bean.QueryCouponState;
import com.jz.community.moduleshoppingguide.home.bean.ShareBean;
import com.jz.community.moduleshoppingguide.home.net.ShoppingGuideReqApi;
import com.jz.community.moduleshoppingguide.nearshop.bean.GoodsInfo;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Tony.Fan on 2018/3/12 17:53
 */
public class ShoppingModelGuideImpl implements ShoppingGuideModel {

    private Context mContext;

    public ShoppingModelGuideImpl(Context mContext) {
        this.mContext = mContext;
    }


    @Override
    public void showCategorysGoodsInfoes(String filter, int page, int size, OnLoadListener<GoodsInfo> onLoadListener) {
        Observable<GoodsInfo> observer = HttpRxObservable.getObservable(ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .showGoodsInfo(filter, page, size));
        new RxLoadingWrapper<GoodsInfo>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }


    @Override
    public void getChunnelCategories(String categoryId, String cityCode, OnLoadListener<List<ChunnelTypeBean>> onLoadListener) {
        Observable<List<ChunnelTypeBean>> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .getChunnelCategories(categoryId, cityCode, AppConstants.APPID));
        new RxLoadingWrapper<List<ChunnelTypeBean>>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void getChunnelCategoriesHome(String cityCode, OnLoadListener<List<ChunnelCategoriesBean>> onLoadListener) {
        Observable<List<ChunnelCategoriesBean>> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .getChunnelCategoriesHome(cityCode, AppConstants.APPID));
        new RxLoadingWrapper<List<ChunnelCategoriesBean>>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }


    @Override
    public void getChannelOutGoods(String id, String shopId, String sort, int page, int size, OnLoadListener<CategoriesGoodsBean> onLoadListener) {
        Observable<CategoriesGoodsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .channelOutCategories(id, shopId, sort, page, size));
        new RxLoadingWrapper<CategoriesGoodsBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });

    }


    @Override
    public void getChunnelHomePage(int page, int size, String cityCode, OnLoadListener<HomeFindBean> onLoadListener) {
        Map<String, Object> map = new HashMap();
        map.put("page", page);
        map.put("size", size);
        map.put("cityCode", cityCode);
        map.put("platformId", AppConstants.APPID);
        Observable<HomeFindBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .getChunnelHomePage(map));
        new RxLoadingWrapper<HomeFindBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> {
                    onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                });
    }

    @Override
    public void getHomeRecommend(int page, int size, OnLoadListener<HomeRecommendBean> onLoadListener) {
        Observable<HomeRecommendBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .getHomeRecommend(page, size, AppConstants.APPID));
        new RxLoadingWrapper<HomeRecommendBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }


    @Override
    public void getChunnelNewborn(int page, int size, OnLoadListener<ChunnelNewbornBean> onLoadListener) {
        Observable<ChunnelNewbornBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .getChunnelNewborn(page, size, AppConstants.APPID));
        new RxLoadingWrapper<ChunnelNewbornBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

    @Override
    public void couponReceiveIds(List<String> couponIds, OnLoadListener<List<QueryCouponState>> onLoadListener) {
        Observable<List<QueryCouponState>> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .couponReceiveIds(new JsonRequestBodyConverter<List<String>>().convert(couponIds)));
        new RxLoadingWrapper<List<QueryCouponState>>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void chunnelShowCategories(String showCategoryId, int page, int size, OnLoadListener<CategoriesGoodsBean> onLoadListener) {
        Observable<CategoriesGoodsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .chunnelShowCategories(showCategoryId, page, size));
        new RxLoadingWrapper<CategoriesGoodsBean>(mContext, true)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void preferentialArea(int page, int size, String cityCode, String message, OnLoadListener<PreferentialBean> onLoadListener) {
        Observable<PreferentialBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .preferentialArea(page, size, cityCode, message));
        new RxLoadingWrapper<PreferentialBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initGoodsData(String cityId, String startTime, String endTime, int page, int size, OnLoadListener<LimitGoodsBean> onLoadListener) {
        Observable<LimitGoodsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .initGoodsData(cityId, startTime, endTime, page, size));
        new RxLoadingWrapper<LimitGoodsBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

    @Override
    public void unreadCount(String type, OnLoadListener<Integer> onLoadListener) {
        Observable<Integer> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .unreadCount(type));
        new RxLoadingWrapper<Integer>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void chunnelHotSaleAll(int page, int size, String showCategoryId, String platformId, int message, OnLoadListener<HotSaleAllBean> onLoadListener) {
        Observable<HotSaleAllBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .chunnelHotSaleAll(page, size, showCategoryId, platformId, message));
        new RxLoadingWrapper<HotSaleAllBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void chunnelHotSaleShow(int message, OnLoadListener<HotSaleShowBean> onLoadListener) {
        Observable<HotSaleShowBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .chunnelHotSaleShow(message));
        new RxLoadingWrapper<HotSaleShowBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initShareGift(OnLoadListener<ShareBean> onLoadListener) {
        Observable<ShareBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, ShoppingGuideReqApi.class)
                .initShareGift());
        new RxLoadingWrapper<ShareBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
